//
//  BaseNavViewController.m
//  TestARC
//
//  Created by 晓泉 吴 on 12-9-22.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BaseNavViewController.h"
#import "BlueButton.h"

#define WAIT_TEXT_TAG 119

@interface BaseNavViewController ()

@end

@implementation BaseNavViewController
@synthesize waitView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self initWaitView];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:waitView];
    [self hideWaitView];
	// Do any additional setup after loading the view.
    
    BlueButton *blueButton = [[BlueButton alloc] init];
    [blueButton addTarget:self action:@selector(backToMain) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *button = [[UIBarButtonItem alloc]
                               initWithCustomView:blueButton];
    self.navigationItem.rightBarButtonItem = button;

//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"药" style:UIBarButtonItemStylePlain target:self action:@selector(backToMain)];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    self.waitView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)backToMain
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)addRoundBound:(UIView *)v
{
    //添加边框
    CALayer *layer = [v layer];
    layer.cornerRadius = 10.0f;
    layer.masksToBounds = YES;
    layer.borderColor = [[UIColor grayColor] CGColor];
    layer.borderWidth = 1.0f;
    //添加四个边阴影
//    layer.shadowColor = [[UIColor blackColor] CGColor];
//    layer.shadowOffset = CGSizeMake(0,0);
//    layer.shadowOpacity = 0.5f;
//    layer.shadowRadius = 10.0f;
}

-(void)initWaitView
{
    float width = 150.0f;
    float hight = 150.0f;
    float left = (320.0f - width)/2.0f;
    float top = 100.0f;
    
    self.waitView = [[UIView alloc]initWithFrame:CGRectMake(left, top, width, hight)];
    self.waitView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
    [self addRoundBound:self.waitView];
    
    float indicatorWidth = 40.0f;
    float indicatorHight = 40.0f;
    float indicatorLeft = 5.0f;
    float indicatorTop = (hight-indicatorHight)/2.0f;
    
    UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(indicatorLeft, indicatorTop, indicatorWidth, indicatorHight)];
    activityIndicatorView.backgroundColor = [UIColor clearColor];
    [activityIndicatorView startAnimating];
    [self.waitView addSubview:activityIndicatorView];
    
    
    float labelWidth = width - indicatorWidth - indicatorLeft*3.0f;
    float labelHight = indicatorHight;
    float labelLeft = indicatorLeft*2.0f+indicatorWidth;
    float labelTop = (hight - labelHight)/2.0f;
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(labelLeft, labelTop, labelWidth, labelHight)];
    label.text = @"正在加载...";
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.tag = WAIT_TEXT_TAG;
    [self.waitView addSubview:label];
    
}
-(void)showWaitView:(NSString *)msg
{
    if (msg.length>0)
    {
        UILabel *label = (UILabel *)[self.waitView viewWithTag:WAIT_TEXT_TAG];
        label.text = msg;
    }
    
    waitView.hidden = NO;
    [self.view bringSubviewToFront:waitView];
}
-(void)hideWaitView
{
    waitView.hidden = YES;
}

@end
